﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace ScoreManage
{
    public partial class TeacherInsert : System.Web.UI.Page
    {
        protected string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserType"].ToString() == "系统管理员")
            {
                ddlDepartment.Enabled = true;
            }
            else
            {
                ddlDepartment.SelectedValue = Session["Department"].ToString();
                ddlDepartment.Enabled = false;
            }
        }
        // 采用直接执行SQL语句的方法实现添加功能
        protected void btnOK_Click(object sender, EventArgs e)    // 添加
        {
            if (Page.IsValid)
            {
                string Tno = txtTno.Text.Trim().Replace("'", "''");
                string Password = txtPassword.Text.Trim().Replace("'", "''");
                string Tname = txtTname.Text.Trim().Replace("'", "''");
                string Sex = rblGender.SelectedValue;
                string Email = txtEmail.Text.Trim().Replace("'", "''");
                string Department = ddlDepartment.SelectedValue;
                

                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();
                string strSql = string.Format("INSERT teacher(tno, password, tname, sex, email, department) VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')", Tno, Password, Tname, Sex, Email, Department);
                SqlCommand myCommand = new SqlCommand(strSql, conn);
                myCommand.ExecuteNonQuery();                            // 执行命令
                Response.Redirect("TeacherManage.aspx?item=3");         // 返回到主页面
            }
        }


        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtTno.Text = "";
            txtPassword.Text = "";
            txtTname.Text = "";
            rblGender.SelectedIndex = -1;
            txtEmail.Text = "";
            if (Session["UserType"].ToString() == "系统管理员")
            {
                ddlDepartment.SelectedIndex = 0;
            }
            else
            {
                ddlDepartment.SelectedValue = Session["Department"].ToString();
            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string Tno = txtTno.Text.Trim().Replace("'", "''");

            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = string.Format("SELECT * FROM teacher WHERE tno ='{0}'", Tno);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataReader dr = myCommand.ExecuteReader();
            args.IsValid = !dr.Read();

        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("TeacherManage.aspx?item=3");
        }
    }
}